package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailMsg;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailMsg.RetAtsRetailMsg;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/29.
 */
public class CITIC02RetailMsg extends CITIC02Base implements IRetailMsg {
    @Override
    public RetAtsRetailMsg getRetailMsg(ReqRetailMsgQry retailMsg) throws Exception {
        RetAtsRetailMsg retAts = new RetAtsRetailMsg(retailMsg.reqHeader);
        retAts.writeStart();
        try {
            CITIC02Header header =  new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCSAMSM";
//            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            HOSTNO	企业端经销商客户代码	VARCHAR	20	Y
            xmlWriterBody.writeElementString("HOSTNO",retailMsg.DealerCode);
//            HOSTNAME	借款企业名称	VARCHAR	122	Y	借款企业名称
            xmlWriterBody.writeElementString("HOSTNAME",retailMsg.DealerName);
//            ORDERNO	交易流水号	VARCHAR	20	Y	企业端零售交易流水号(批次号)
            // TODO: 2019/11/4 长安汽车不会把订单号推送过来，和银行商定随便传个唯一值
            xmlWriterBody.writeElementString("ORDERNO",reqHeader.transSeqID);
//            OPRNO	操作人编号	VARCHAR	20	N
            xmlWriterBody.writeElementString("OPRNO","");
//            OPRNM	操作人名称	VARCHAR	30	N
            xmlWriterBody.writeElementString("OPRNM","");
//            FIELD1	属性1	VARCHAR	30	N
            xmlWriterBody.writeElementString("FIELD1","");
//            FIELD2	属性2	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2","");
//            FIELD3	属性3	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD3","");
//            FIELD4	属性4	DECIMAL	19,2	N
            xmlWriterBody.writeElementString("FIELD4","");
//            FIELD5	属性5	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD5","");
//            LIST	汽车列表	LIST
            xmlWriterBody.writeStartElement("SPVWHCMDLST");

            while (retailMsg.readRecord()) {
                xmlWriterBody.writeStartElement("row");
//                VIN	车架号	VARCHAR	20	Y
                xmlWriterBody.writeElementString("VIN",retailMsg.ReqDetail.VIN);
//                CERTNO	合格证号	VARCHAR	60	Y
                xmlWriterBody.writeElementString("CERTNO",retailMsg.ReqDetail.CertificationNo);
//                CLASS	车型	VARCHAR	100	N
                xmlWriterBody.writeElementString("CLASS","");
//                COLOUR	颜色	VARCHAR	20	N
                xmlWriterBody.writeElementString("COLOUR","");
//                ENGINENO	发动机号	VARCHAR	30	Y
                xmlWriterBody.writeElementString("ENGINENO",retailMsg.ReqDetail.EngineNo);
//                PRICE	实销价格	DECIMAL	19,2	Y
                // TODO: 2019/11/4 长安汽车没有金额推过来，暂时退0个体银行
                xmlWriterBody.writeElementString("PRICE","0");
//                STATUS	汽车状态	VARCHAR	10	Y	默认为已出库
                // TODO: 2019/9/29
                xmlWriterBody.writeElementString("STATUS","6");
//                SALEDATE	销售日期	VARCHAR	30	Y
                xmlWriterBody.writeElementString("SALEDATE", DateHelper.convertD10toD8(retailMsg.ReqDetail.RetailTime));
//                INVOICEDATE	开票日期	VARCHAR	30	N
                xmlWriterBody.writeElementString("INVOICEDATE","");
//                FIELD6	属性6	VARCHAR	50	N
                xmlWriterBody.writeElementString("FIELD6","");
//                FIELD7	属性7	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD7","");
//                FIELD8	属性8	DECIMAL	19,2	N
                xmlWriterBody.writeElementString("FIELD8","");
//                FIELD9	属性9	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD9","");
//                FIELD10	属性10	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD10","");
                xmlWriterBody.writeEndElement();
            }
//                    LIST_END
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(),header);
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String retCode =tmpBkReader.getFirstText("/stream/RETCODE");
                    if("AAAAAAA".equals(retCode)){
                        retAts.respCode = "0";
                    }else{
                        retAts.respCode = "-1";
                    }
                    retAts.respInfo = retCode;
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
